package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Claseasignatura;
import com.lostreseditores.sabersai.model.dto.ClaseasignaturaDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class ClaseasignaturaView {
    private InputText txtClasasigdescript;
    private InputText txtClasasigidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<ClaseasignaturaDTO> data;
    private ClaseasignaturaDTO selectedClaseasignatura;

    public ClaseasignaturaView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            ClaseasignaturaDTO claseasignaturaDTO = (ClaseasignaturaDTO) e.getObject();

            if (txtClasasigdescript == null) {
                txtClasasigdescript = new InputText();
            }

            txtClasasigdescript.setValue(claseasignaturaDTO.getClasasigdescript());

            if (txtClasasigidn == null) {
                txtClasasigidn = new InputText();
            }

            txtClasasigidn.setValue(claseasignaturaDTO.getClasasigidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtClasasigdescript != null) {
            txtClasasigdescript.setValue(null);
            txtClasasigdescript.setDisabled(true);
        }

        if (txtClasasigidn != null) {
            txtClasasigidn.setValue(null);
            txtClasasigidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Claseasignatura entity = null;

        try {
            Integer clasasigidn = new Integer(txtClasasigidn.getValue()
                                                            .toString());
            entity = BusinessDelegatorView.getClaseasignatura(clasasigidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtClasasigdescript.setDisabled(false);
            txtClasasigidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtClasasigdescript.setValue(entity.getClasasigdescript());
            txtClasasigdescript.setDisabled(false);
            txtClasasigidn.setValue(entity.getClasasigidn());
            txtClasasigidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveClaseasignatura(FacesUtils.checkString(
                    txtClasasigdescript),
                FacesUtils.checkInteger(txtClasasigidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteClaseasignatura(FacesUtils.checkInteger(
                    txtClasasigidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateClaseasignatura(FacesUtils.checkString(
                    txtClasasigdescript),
                FacesUtils.checkInteger(txtClasasigidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtClasasigidn == null) {
                txtClasasigidn = new InputText();
            }

            txtClasasigidn.setValue(selectedClaseasignatura.getClasasigidn());

            BusinessDelegatorView.deleteClaseasignatura(FacesUtils.checkInteger(
                    txtClasasigidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedClaseasignatura);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String clasasigdescript,
        Integer clasasigidn) throws Exception {
        try {
            BusinessDelegatorView.updateClaseasignatura(clasasigdescript,
                clasasigidn);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("ClaseasignaturaView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtClasasigdescript() {
        return txtClasasigdescript;
    }

    public void setTxtClasasigdescript(InputText txtClasasigdescript) {
        this.txtClasasigdescript = txtClasasigdescript;
    }

    public InputText getTxtClasasigidn() {
        return txtClasasigidn;
    }

    public void setTxtClasasigidn(InputText txtClasasigidn) {
        this.txtClasasigidn = txtClasasigidn;
    }

    public List<ClaseasignaturaDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataClaseasignatura();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<ClaseasignaturaDTO> claseasignaturaDTO) {
        this.data = claseasignaturaDTO;
    }

    public ClaseasignaturaDTO getSelectedClaseasignatura() {
        return selectedClaseasignatura;
    }

    public void setSelectedClaseasignatura(ClaseasignaturaDTO claseasignatura) {
        this.selectedClaseasignatura = claseasignatura;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
